<?php


namespace app\admin\controller;

use app\admin\model\FlashSale;
use app\admin\model\Goods;
use app\admin\model\GoodsActivity;
use app\admin\model\GroupBuy;
use app\common\model\PromGoods;
use think\AjaxPage;
use think\Page;
use app\admin\logic\GoodsLogic;
use think\Loader;
use think\Db;

class Video extends Base
{

    public function index()
    {
        return $this->fetch();
    }

    /**
     * 商品活动列表
     */
    public function video_list()
    {	
		$count = Db::query("select count(vid) from tp_video")[0]['count(vid)'];
		$Page = new Page($count, 10);
		$videolist = Db::query("select * from tp_video order by vid desc LIMIT ?,?",
		[$Page->firstRow,$Page->listRows]);	
		
		foreach($videolist as $key=>$values){
			if($values['showtype']==1){
				$videolist[$key]['showtypename'] = '首页推荐';
			}
			else{
				$videolist[$key]['showtypename'] = '视频列表';
			}
		}
		
		$this->assign('page',$Page);
		$this->assign('videolist', $videolist);
        return $this->fetch();
    }
	
	
	public function video_info()
    {
    	$v_id = I('vid');
    	if($v_id > 0) {
            $info = M('video')->where("vid=$v_id")->find();
			
			$video_goods = M('Goods')->whereIn('goods_id',$info['goods_id'])->select();

			$this->assign('video_goods', $video_goods);
			$this->assign('info', $info);
        }
		
        return $this->fetch();
    }
	
	public function search_goods()
    {
        $goods_id = input('goods_id');
        $intro = input('intro');
        $cat_id = input('cat_id');
        $brand_id = input('brand_id');
        $keywords = input('keywords');
        $tpl = input('tpl', 'search_goods');
        $where = ['is_on_sale' => 1, 'store_count' => ['gt', 0],'is_virtual'=>0,'exchange_integral'=>0];
        $prom_type = input('prom_type/d');
        if($goods_id){
            $where['goods_id'] = ['<>',$goods_id];
        }
        if($intro){
            $where[$intro] = 1;
        }
        if($cat_id){
            $grandson_ids = getCatGrandson($cat_id);
            $where['cat_id'] = ['in',implode(',', $grandson_ids)];
        }
        if ($brand_id) {
            $where['brand_id'] = $brand_id;
        }
        if($keywords){
            $where['goods_name|keywords'] = ['like','%'.$keywords.'%'];
        }
		
        $Goods = new Goods();
        $count = $Goods->where($where)->count();
        $Page = new Page($count, 10);
        $goodsList = $Goods->where($where)->order('goods_id DESC')->limit($Page->firstRow . ',' . $Page->listRows)->select();
		
//		return json_encode($goodsList);
		
        $GoodsLogic = new GoodsLogic;
        $brandList = $GoodsLogic->getSortBrands();
        $categoryList = $GoodsLogic->getSortCategory();
        $this->assign('brandList', $brandList);
        $this->assign('categoryList', $categoryList);
        $this->assign('page', $Page);
        $this->assign('goodsList', $goodsList);
        return $this->fetch($tpl);
    }
	
	/**
	 *添加或者编辑视频信息 
	 */
	public function video_info_save(){
		
		$v_id = I('vid/d');
        $data = I('post.');
        $vGoods = I('goods/a',[]);//$data['goods'];
		
        $videoValidate = Loader::validate('VideoValidate');
		

		if(!$videoValidate->batch()->check($data)){
            $error = '';
			$errresult = $videoValidate->getError();
            foreach ($videoValidate->getError() as $value){
                $error .= $value.'！';
            }
            $this->ajaxReturn(['status' => 0,
            'msg' =>$error,
            'result'    => $errresult]);
        }
		
		$goods_ids = [];
        foreach ($vGoods as $goodsKey => $goodsVal) {
            if (array_key_exists('goods_id', $goodsVal)) {
                array_push($goods_ids, $goodsVal['goods_id']);
            }
        }

		
		if(count($goods_ids)<=0){
			$this->ajaxReturn(['status' => 0,'msg' =>'请选择视频对应的商品',$goods_ids]);
		}
		
		$data['goods_id'] = implode(',', $goods_ids);
		
		if ($v_id) {
			
			if($data['showtype'] == 1){
				Db::query("update tp_video set showtype = 2");
			}
			
            M('video')->where("vid=$v_id")->save($data);
              adminLog("管理员修改了视频 " . I('vname'));
        } else {
            $data['addtime'] = time();
            M('video')->add($data);			
            adminLog("管理员添加了视频 " . I('vname'));
        }
//		$this->success('保存成功', U('Video/video_list'));
		$this->ajaxReturn(['status' => 1,
            'msg' =>'保存成功']);
	}
	
	public function video_info_del()
    {
        $v_id = I('vid');
        
		$video = M('video')->where("vid=$v_id")->find();
		
		if (empty($video)) {
            $this->error("视频不存在!");
        }
		
        M('video')->where("vid=$v_id")->delete();
		adminLog("管理员删除了视频 " . $video['vname']);
//      $this->success('删除视频成功', U('Video/video_list'));		
		$this->ajaxReturn(['status' => 1,
            'msg' =>'删除成功']);
    }
	
	//评论列表
	public function video_comm_list(){
		$v_id = I('vid/d',0);
		
		$video = M('video')->where("vid=$v_id")->find();
		
		if (empty($video)) {
            $this->error("视频不存在!");
        }
		
		$count = Db::query("select count(id) from tp_video_comment where vid = $v_id")[0]['count(id)'];
		$Page = new Page($count, 10);
		$vcommlist = Db::query("select tp_video_comment.*,tp_users.nickname from tp_video_comment,tp_users 
		where tp_video_comment.vid = $v_id and tp_video_comment.uid = tp_users.user_id order by addtime desc LIMIT ?,?",
		[$Page->firstRow,$Page->listRows]);	
		
		
		$this->assign('page',$Page);	
		$this->assign('vname',$video['vname']);		
		$this->assign('videolist', $vcommlist);
        return $this->fetch();
	}
	
	//删除评论
	public function video_comm_del(){
		$id = I('id/d',0);
        
//		return json_encode(['id'=>$id]);
		
		$video = M('video_comment')->where("id=$id")->find();
		
		$vid = 0;
		if (!empty($video)) {
            $vid = $video['vid'];
        }
		
        M('video_comment')->where("id=$id")->delete();
		adminLog("管理员删除了视频 评论--  ". $vid);
		$this->ajaxReturn(['status' => 1,'msg' =>'删除成功']);
	}
	
	
	public function search_more_goods()
    {
        $goods_id = input('goods_id');
        $intro = input('intro');
        $cat_id = input('cat_id');
        $brand_id = input('brand_id');
        $keywords = input('keywords');
        $prom_id = input('prom_id');
        $tpl = input('tpl', 'search_goods');
        $where = ['is_on_sale' => 1, 'store_count' => ['gt', 0],'is_virtual'=>0,'exchange_integral'=>0];
        $prom_type = input('prom_type/d');
        if($goods_id){
            $where['goods_id'] = ['<>',$goods_id];
        }
        if($intro){
            $where[$intro] = 1;
        }
        if($cat_id){
            $grandson_ids = getCatGrandson($cat_id);
            $where['cat_id'] = ['in',implode(',', $grandson_ids)];
        }
        if ($brand_id) {
            $where['brand_id'] = $brand_id;
        }
        if($keywords){
            $where['goods_name|keywords'] = ['like','%'.$keywords.'%'];
        }
        $Goods = new Goods();
        $count = $Goods->where($where)->where(function ($query) use ($prom_type, $prom_id) {
            if($prom_type == 3){
                //优惠促销
                if ($prom_id) {
                    $query->where(['prom_id' => $prom_id, 'prom_type' => 3])->whereor('prom_type', 0);
                } else {
                    $query->where('prom_type', 0);
                }
            }else if(in_array($prom_type,[1,2,6])){
                //抢购，团购
                $query->where('prom_type','in' ,[0,$prom_type])->where('prom_type',0);
            }else{
                $query->where('prom_type',0);
            }
        })->count();
        $Page = new Page($count, 10);
        $goodsList = $Goods->with('specGoodsPrice')->where($where)->where(function ($query) use ($prom_type, $prom_id) {
            if($prom_type == 3){
                //优惠促销
                if ($prom_id) {
                    $query->where(['prom_id' => $prom_id, 'prom_type' => 3])->whereor('prom_type', 0);
                } else {
                    $query->where('prom_type', 0);
                }
            }else if(in_array($prom_type,[1,2,6])){
                //抢购，团购
                $query->where('prom_type','in' ,[0,$prom_type]);
            }else{
                $query->where('prom_type',0);
            }
        })->order('goods_id DESC')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $GoodsLogic = new GoodsLogic;
        $brandList = $GoodsLogic->getSortBrands();
        $categoryList = $GoodsLogic->getSortCategory();
        $this->assign('brandList', $brandList);
        $this->assign('categoryList', $categoryList);
        $this->assign('page', $Page);
        $this->assign('goodsList', $goodsList);
		return $this->fetch();
        //return $this->fetch($tpl);
    }
}